package pe.com.bbva.modpre.ui;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import pe.com.bbva.modpre.dominio.Usuario;
import pe.com.bbva.modpre.exception.ValidacionException;
import pe.com.bbva.modpre.negocio.SeguridadService;
import pe.com.bbva.modpre.utileria.Constante;
import pe.com.bbva.modpre.utileria.Inject;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.event.ShortcutListener;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

@SuppressWarnings("serial")
public class LoginUI extends CustomComponent implements ClickListener {

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	@AutoGenerated
	private VerticalLayout mainLayout;

	@AutoGenerated
	private VerticalLayout verticalLayout_2;

	@AutoGenerated
	private Panel pnlLogin;

	@AutoGenerated
	private VerticalLayout layoutLogin;

	@AutoGenerated
	private HorizontalLayout pnlBotonera;

	@AutoGenerated
	private Button btnLogin;

	@AutoGenerated
	private VerticalLayout pnlLogo;

	@AutoGenerated
	private PasswordField txtClave;

	@AutoGenerated
	private TextField txtUsuario;

	@AutoGenerated
	private VerticalLayout verticalLayout_3;

	@AutoGenerated
	private HorizontalLayout lytBarra;

	@AutoGenerated
	private HorizontalLayout horizontalLayout_1;

	@AutoGenerated
	private Embedded imgBbva2;

	@AutoGenerated
	private Label label_1;

	@AutoGenerated
	private Embedded imgBbva;

	@Autowired
	private SeguridadService seguridadService;
	
	@Override
	public void attach() {
		super.attach();
		//getWindow().executeJavaScript("aplicarValidaciones(9);");
	}

	public LoginUI() {		
		buildMainLayout();
		setCompositionRoot(mainLayout);
		Inject.inject(this);
		btnLogin.addListener((ClickListener)this);
		
		txtUsuario.addShortcutListener(new ShortcutListener("", KeyCode.ENTER, null){

			private static final long serialVersionUID = 1L;

			@Override
			public void handleAction(Object sender, Object target) {
				handleActionEnter();
			}
			
		});
		txtClave.addShortcutListener(new ShortcutListener("", KeyCode.ENTER, null){

			private static final long serialVersionUID = 1L;

			@Override
			public void handleAction(Object sender, Object target) {
				handleActionEnter();
			}
			
		});
		debugId();
	}
	
	private void debugId(){
		mainLayout.setDebugId("mainLayout");
		pnlLogin.setDebugId("pnlLogin");
		layoutLogin.setDebugId("layoutLogin");
		pnlBotonera.setDebugId("pnlBotonera");
		btnLogin.setDebugId("btnLogin");
		txtClave.setDebugId("txtClave");
		txtUsuario.setDebugId("txtUsuario");
		pnlLogo.setDebugId("pnlLogo");
	}
	
	private void handleActionEnter()
	{
		buttonClickLogin();
	}
	
	@Override
	public void buttonClick(ClickEvent event) {
		if(event.getButton().equals(btnLogin))
		{
			buttonClickLogin();
		}
	}
	
	@SuppressWarnings("deprecation")
	private void buttonClickLogin()
	{
		
		
		String username = txtUsuario.getValue().toString().trim();
		String password = txtClave.getValue().toString().trim();
        
        if(StringUtils.isBlank(username))
        {
        	seguridadService.resolverAlertaException(
        			new ValidacionException(Constante.CODIGO_MENSAJE.VALIDAR_TEXTBOX, new Object[]{"Usuario"}), 
        			getApplication().getMainWindow().getWindow());
			return;
        }
        
        if(StringUtils.isBlank(password))
        {
        	seguridadService.resolverAlertaException(
        			new ValidacionException(Constante.CODIGO_MENSAJE.VALIDAR_TEXTBOX, new Object[]{"Password"}), 
        			getApplication().getMainWindow().getWindow());
			return;
        }
        
        txtUsuario.setValue(username);
        
        username = username.toUpperCase();
        
        Usuario usuario = seguridadService.login(username, password, getApplication().getMainWindow().getWindow());
        
        if(usuario != null)
        {
        	
        	ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
    				.currentRequestAttributes();
    		HttpServletRequest request = requestAttributes.getRequest();
    		HttpSession session = request.getSession(true);
    		session.setAttribute(Constante.SESION.USUARIO, usuario);
    		Window windowModpre=getApplication().getMainWindow();
    		windowModpre.removeAllComponents();
    		windowModpre.setCaption("Modelo de Precios");
    		PrincipalUI principalUI=new PrincipalUI();
    		windowModpre.addComponent(principalUI);
    		windowModpre.getContent().setHeight("100%");
    		windowModpre.getLayout().setMargin(false);
        }
	}

	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");
		
		// verticalLayout_2
		verticalLayout_2 = buildVerticalLayout_2();
		mainLayout.addComponent(verticalLayout_2);
		
		return mainLayout;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_2() {
		// common part: create layout
		verticalLayout_2 = new VerticalLayout();
		verticalLayout_2.setImmediate(false);
		verticalLayout_2.setWidth("100.0%");
		verticalLayout_2.setHeight("-1px");
		verticalLayout_2.setMargin(false);
		
		// lytBarra
		lytBarra = buildLytBarra();
		verticalLayout_2.addComponent(lytBarra);
		
		// verticalLayout_3
		verticalLayout_3 = new VerticalLayout();
		verticalLayout_3.setImmediate(false);
		verticalLayout_3.setWidth("-1px");
		verticalLayout_3.setHeight("50px");
		verticalLayout_3.setMargin(false);
		verticalLayout_2.addComponent(verticalLayout_3);
		
		// pnlLogin
		pnlLogin = buildPnlLogin();
		verticalLayout_2.addComponent(pnlLogin);
		verticalLayout_2.setComponentAlignment(pnlLogin, new Alignment(20));
		
		return verticalLayout_2;
	}

	@AutoGenerated
	private HorizontalLayout buildLytBarra() {
		// common part: create layout
		lytBarra = new HorizontalLayout();
		lytBarra.setStyleName("lytLogo");
		lytBarra.setImmediate(false);
		lytBarra.setWidth("100.0%");
		lytBarra.setHeight("129px");
		lytBarra.setMargin(false);
		
		// imgBbva
		imgBbva = new Embedded();
		imgBbva.setImmediate(false);
		imgBbva.setWidth("167px");
		imgBbva.setHeight("129px");
		imgBbva.setSource(new ThemeResource("img/log_01.gif"));
		imgBbva.setType(1);
		imgBbva.setMimeType("image/png");
		lytBarra.addComponent(imgBbva);
		
		// horizontalLayout_1
		horizontalLayout_1 = buildHorizontalLayout_1();
		lytBarra.addComponent(horizontalLayout_1);
		lytBarra.setComponentAlignment(horizontalLayout_1, new Alignment(6));
		
		return lytBarra;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_1() {
		// common part: create layout
		horizontalLayout_1 = new HorizontalLayout();
		horizontalLayout_1.setImmediate(false);
		horizontalLayout_1.setWidth("-1px");
		horizontalLayout_1.setHeight("-1px");
		horizontalLayout_1.setMargin(false);
		
		// label_1
		label_1 = new Label();
		label_1.setStyleName("labelTitulo");
		label_1.setImmediate(false);
		label_1.setWidth("-1px");
		label_1.setHeight("-1px");
		label_1.setValue("Modelo de Precios");
		horizontalLayout_1.addComponent(label_1);
		
		// imgBbva2
		imgBbva2 = new Embedded();
		imgBbva2.setImmediate(false);
		imgBbva2.setWidth("207px");
		imgBbva2.setHeight("129px");
		imgBbva2.setSource(new ThemeResource("img/log_04.gif"));
		imgBbva2.setType(1);
		imgBbva2.setMimeType("image/png");
		horizontalLayout_1.addComponent(imgBbva2);
		
		return horizontalLayout_1;
	}

	@AutoGenerated
	private Panel buildPnlLogin() {
		// common part: create layout
		pnlLogin = new Panel();
		pnlLogin.setStyleName("login");
		pnlLogin.setCaption("Ingreso al Sistema");
		pnlLogin.setImmediate(false);
		pnlLogin.setWidth("400px");
		pnlLogin.setHeight("160px");
		
		// layoutLogin
		layoutLogin = buildLayoutLogin();
		pnlLogin.setContent(layoutLogin);
		
		return pnlLogin;
	}

	@AutoGenerated
	private VerticalLayout buildLayoutLogin() {
		// common part: create layout
		layoutLogin = new VerticalLayout();
		layoutLogin.setImmediate(false);
		layoutLogin.setWidth("100.0%");
		layoutLogin.setHeight("100.0%");
		layoutLogin.setMargin(false);
		
		// pnlLogo
		pnlLogo = buildPnlLogo();
		layoutLogin.addComponent(pnlLogo);
		layoutLogin.setComponentAlignment(pnlLogo, new Alignment(24));
		
		// pnlBotonera
		pnlBotonera = buildPnlBotonera();
		layoutLogin.addComponent(pnlBotonera);
		layoutLogin.setComponentAlignment(pnlBotonera, new Alignment(20));
		
		return layoutLogin;
	}

	@AutoGenerated
	private VerticalLayout buildPnlLogo() {
		// common part: create layout
		pnlLogo = new VerticalLayout();
		pnlLogo.setImmediate(false);
		pnlLogo.setWidth("350px");
		pnlLogo.setHeight("70px");
		pnlLogo.setMargin(false);
		
		// txtUsuario
		txtUsuario = new TextField();
		txtUsuario.setStyleName("textLogin");
		txtUsuario.setImmediate(false);
		txtUsuario.setWidth("330px");
		txtUsuario.setHeight("-1px");
		txtUsuario.setInputPrompt("Usuario");
		pnlLogo.addComponent(txtUsuario);
		
		// txtClave
		txtClave = new PasswordField();
		txtClave.setStyleName("textLogin");
		txtClave.setImmediate(false);
		txtClave.setWidth("330px");
		txtClave.setHeight("-1px");
		txtClave.setInputPrompt("Clave");
		pnlLogo.addComponent(txtClave);
		
		return pnlLogo;
	}

	@AutoGenerated
	private HorizontalLayout buildPnlBotonera() {
		// common part: create layout
		pnlBotonera = new HorizontalLayout();
		pnlBotonera.setImmediate(false);
		pnlBotonera.setWidth("350px");
		pnlBotonera.setHeight("-1px");
		pnlBotonera.setMargin(false);
		
		// btnLogin
		btnLogin = new Button();
		btnLogin.setStyleName("buttonBbva");
		btnLogin.setCaption("Ingresar");
		btnLogin.setImmediate(true);
		btnLogin.setWidth("-1px");
		btnLogin.setHeight("-1px");
		pnlBotonera.addComponent(btnLogin);
		pnlBotonera.setComponentAlignment(btnLogin, new Alignment(6));
		
		return pnlBotonera;
	}	

}
